<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Handler_Community");
class Action_Community_RssModeratePublish extends Handler_Community {
  public function process() {
    $aa = $this->aa;
		$acl_data = $aa->getInfo();
		if (!$aa->is_granted('community_edit')) return;
    
    if ( isset($this->params['oid']) && !empty($this->params['oid']) ) {
      $object = $this->ctx->store->getObject($this->params['oid']);
      
  		$info['community_oid'] = $object->data['beplaced_community_oid'];
      if ( !empty($info['tdi_oid']) ) {
        $info['tdi_oid'] = $object->data['beplaced_tdi_oid'];
      }
			$info['title'] = $object->data['title'];
			$info['body'] = $object->data['body'];
      $info['is_deleted'] = 0;
      
      $item = $this->ctx->store->create('fdoc', $info);
      $item->save();
    	$item->inSense("doc");
      $item->inSense("allfeed");
    	$item->save();
      
      if ( !empty($info['tdi_oid']) ) {
        $aa->createItem( $item->id, $info['tdi_oid'] );
      } else {
        $aa->createItem( $item->id, $info['community_oid'] );
      }
      
      $object->is_deleted = 1;
      $object->updateSense('allfeed');
      $object->save();
      
      $this->hashItem( $item->id );
    }
    
    $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/rss_moderate";
    if ( isset($this->params['lpage']) && !empty($this->params['lpage']) ) {
      $lpage = (integer) abs($this->params['lpage']+0);
      if ( $lpage > 0 ) {
        $location .= "?lpage={$lpage}";
      }
    } else if ( isset($this->params['show_all']) && !empty($this->params['show_all']) ) {
      $location .= "?show_all=1";
    }
    header('Location: ' . $location);
    exit;
  }
  
  public function hashItem( $oid ) {
    $obj = $this->ctx->store->getObject($oid);
    
    $this->rh->useClass('Search_Types');
    $searchTypes = new Search_Types($this->rh, $this->ctx->store);
    
    $parser = $searchTypes->getTypeParser($obj->type);
    if ( false === $parser ) {
      //print "Can`t find parser for '{$obj->type}' type.\n";
      return;
    }
    
    $parser->linkFromObject($obj);
    $maked = $parser->makeSearchData($obj->id);
    if ( !$maked ) {
      //print "Can`t index item with '{$obj->id}' oid.";
      return;
    }
    
    //$parser->cleanWords();
  }
}